<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use auth\Auth;

class Base extends  Controller
{
    public $account;
    public $uid;
    
    public function _initialize() {
        // 判定用户是否登录
        $isLogin = $this->isLogin();
        
//        if(!$isLogin) {
//            return $this->redirect(url('login/index'));
//        }
        
        if($isLogin) {
            $groupId = model('AuthUserGroup')->get(['uid'=>$isLogin])->uid;
            // 如果不是超级管理员组，则需要节点认证
            if($groupId != 1) {
                $module = request()->module();
                $controller = request()->controller();
                $action = request()->action();
                $auth = new Auth();
                // Auth类对节点进行认证
                $authCheck = $auth->check($module . '/' . $controller . '/' . $action, $isLogin);
                if(!$authCheck){
                    $this->redirect(url('admin/index/accessdenied'));
                }
            }
        } else {
            $this->redirect(url('login/index'));
        }
    }

    //判定是否登录
    public function isLogin() {
        // 获取sesssion
        $user = $this->getLoginUser();
        if($user && $user->id) {
            return $user->id;
        }
        return false;

    }

    public function getLoginUser() {
        if(!$this->account) {
            $this->account = session('AuthUser', '', 'admin');
        }
        return $this->account;
    }

}
